<?php
    class Admin_Model_Common
    {
        public function convertDateToString($date) {
            $str = explode('-', $date);
            $str = implode('', $str);
            return $str;
        }
        
        public function convertToMysql($date) {
            $date = explode("-",$date);
            if ($date[0]<=9) { $date[0]="0".$date[0]; }
            if ($date[1]<=9) { $date[1]="0".$date[1]; }
            $date = array($date[2], $date[1], $date[0]);
            return $n_date=implode("-", $date);
        }
        
        public function convertToTime($date) {
            $date = explode("-",$date);
            $date = array($date[2], $date[1], $date[0]);
            return $n_date=implode("-", $date);
        }
        
        public function formatMoney($number, $fractional=false) {
            if($fractional) {
                $number = sprintf('%f', $number);
            }
            while(true) {
                $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1.$2', $number);
                if($replaced != $number) {
                    $number = $replaced;
                }
                else {
                    break;
                }
            }
            return $number;
        }
        
        public function cut_string($str,$len,$more) {
            if ($str=='' || $str==NULL) return $str;
            if (is_array($str)) return $str;
            $str = trim($str);
            if (strlen($str) <= $len) return $str;
            $str = substr($str,0,$len);
            if ($str != '') {
                if (!substr_count($str," ")) {
                    if ($more) $str .= " ...";
                    return $str;
                }
                while(strlen($str) && ($str[strlen($str)-1] != " ")) $str = substr($str,0,-1);
                $str = substr($str,0,-1);
                if ($more) $str .= " ...";
            }
            return $str;
        }
        
        public function removeHtmlTag($string) {
            $string = preg_replace('/<[a-zA-Z\/][^>]*>/i', ' ', $string);
            $string = preg_replace('/&\w+;/i', ' ', $string);
            $string = preg_replace('/\s{2,}/', ' ', $string);
            return $string;
        }
    }
